package org.heydr.entity;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.heydr.common.http.Proxy;

/**
 * http snatch param,the param url is required,optional for other else
 */
public class HttpSnatchParam implements Cloneable{
	
	/* url */
	private String url;

	/* whether to trace the redirect url or not */
	private boolean redirecting = true;

	/* whether need to be encoded(default:true) * */
	private boolean needEncodeUrl = true;

	/* url parameters encode type(default:utf-8) */
	private String urlEncoding = "utf-8";

	/* request header */
	private List<Header> headers = new LinkedList<Header>();

	/* proxy */
	private Proxy proxy;

	/* post parameters,request in post method when it's not null,or it would request in get method */
	private List<NameValuePair> postParams = new LinkedList<NameValuePair>();

	/* post parameters encoding,it is necessary when postParams is not null */
	private String postParamsEncoding;

	/* resource content minimum length limit(unit:byte),unlimit when minLengthLimit<=0 */
	private long minLengthLimit;

	/* resource content maximum length limit(unit:byte),unlimit when minLengthLimit<=0 */
	private long maxLengthLimit;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List<NameValuePair> getPostParams() {
		return postParams;
	}

	public void setPostParams(List<NameValuePair> postParams) {
		this.postParams.addAll(postParams);
	}

	public void addPostParam(String name, String value) {
		if (name != null) {
			NameValuePair param = new BasicNameValuePair(name, value);
			this.postParams.add(param);
		}
	}

	public String getPostParamsEncoding() {
		return postParamsEncoding;
	}

	public void setPostParamsEncoding(String postParamsEncoding) {
		this.postParamsEncoding = postParamsEncoding;
	}

	public List<Header> getRequestHeaders() {
		return headers;
	}

	public void addRequestHeaders(Header[] addRequestHeaders) {
		if (addRequestHeaders != null) {
			for (Header hd : addRequestHeaders) {
				this.headers.add(hd);
			}
		}
	}

	public void addRequestHeaders(List<Header> headers) {
		this.headers.addAll(headers);
	}

	public void addRequestHeader(String name, String value) {
		if (name != null) {
			Header hd = new BasicHeader(name, value);
			this.headers.add(hd);
		}
	}

	public Proxy getProxy() {
		return proxy;
	}

	public void setProxy(Proxy proxy) {
		this.proxy = proxy;
	}

	public void setRedirecting(boolean redirecting) {
		this.redirecting = redirecting;
	}

	public boolean isRedirecting() {
		return redirecting;
	}

	public boolean isNeedEncodeUrl() {
		return needEncodeUrl;
	}

	public void setNeedEncodeUrl(boolean needEncodeUrl) {
		this.needEncodeUrl = needEncodeUrl;
	}

	public String getUrlEncoding() {
		return urlEncoding;
	}

	public void setUrlEncoding(String urlEncoding) {
		this.urlEncoding = urlEncoding;
	}
	
	public long getMinLengthLimit() {
		return minLengthLimit;
	}

	public void setMinLengthLimit(long minLengthLimit) {
		this.minLengthLimit = minLengthLimit;
	}

	public long getMaxLengthLimit() {
		return maxLengthLimit;
	}

	public void setMaxLengthLimit(long maxLengthLimit) {
		this.maxLengthLimit = maxLengthLimit;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

	public List<Header> getHeaders() {
		return headers;
	}

	public void setHeaders(List<Header> headers) {
		this.headers = headers;
	}
	
}
